<?php

namespace frontend\models;

use Yii;
use yii\base\Model;

/**
 * ContactForm is the model behind the contact form.
 */
class Anonymous extends Model
{
    public $name;
    public $signature;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [

            [['name'], 'required','message'=>'随便起个名字吧：)'],
            [['name'], 'string', 'max' => 15],
            [['signature'], 'string', 'max' => 30]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'name' => '匿名:',
            'signature' => '签名:',
        ];
    }

    /**
     * Sends an email to the specified email address using the information collected by this model.
     *
     * @param  string $email the target email address
     * @return boolean whether the email was sent
     */
    public function login()
    {
        if($this->validate()){
            $cookies = Yii::$app->response->cookies;
            $cookies->add(new \yii\web\Cookie([
                'name' => 'anonymous_name',
                'value' => htmlspecialchars($this->name),
                'expire' => time() + 86400 * 365,
            ]));
            $cookies->add(new \yii\web\Cookie([
                'name' => 'anonymous_signature',
                'value' => htmlspecialchars($this->signature),
                'expire' => time() + 86400 * 365,
            ]));
            return true;
        }
        return false;
    }
}
